const express = require('express')
const passport = require('passport')
const router = express.Router()
const db = require('../../db')
router.post('/pubnotice', passport.authenticate('jwt', {session: false}), (req, res) => {
    if(req.user.role !== 1) return res.status(802).json({status: 1, message: '非管理无法发布通知'})
    const sqlStr = 'insert into notices set ?'
    const obj = {
        user_id : req.user.id,
        username: req.user.name,
        content: req.body.content
    }
    db.query(sqlStr, obj, (err, results) => {
        if(err) res.status(500).json({status: 1, message: err.message})
        res.json({status: 0, message: '发布成功'})
    })
})

router.get('/getnotices', passport.authenticate('jwt', {session: false}), (req, res) => {
    const sqlStr = 'select * from notices'
    db.query(sqlStr, (err, results) => {
        if(err) return res.status(500).json({status: 1, message: err.message})
        if(results.length === 0) return res.json({status: 1, message: '暂无任何通知'})
        res.json({status: 0, message: '获取成功', data: results})
    })
})

module.exports = router

